<?php 
define('IN_WONDERSHARE', true);
header("Content-type:text/html;charset=utf-8");
ob_start("ob_gzhandler");
require('../includes/init.php');
require('./classes/control/control_category.php');
require ('./configs/site_config.php'); 
class Category 
{
	var $category_c;
	function __construct(){
	
		$this->category_c = new CategoryControl();
	}
	//类别管理
	function index()
	{
		$w_type = empty($_REQUEST['w_type'])?0:(int)$_REQUEST['w_type'];//网站类型
		if(!empty($_GET['type']))//为话题分类
		{
			$where = ' WHERE c.cat_web = '.$w_type.' AND c.cat_type =  '.(int)$_GET['type'];
		}
		if($_REQUEST['jax_type'])
		{
			switch ($_REQUEST['jax_type'])
			{
				case 'sort_order'://ajax修改排序
					$this->category_c->change_sort((int)$_GET['cat_id'],$_GET['sort_order']);
				break;  
				
				case 'parent_series'://得到上级分类是几级  
					$series  = $this->category_c->get_category_info($w_type,(int)$_GET['cat_id'],
					$_GET['parent_id'],'','series','',$_GET['cat_series']);
					echo  $series;
				break;
			
				case 'repeat_name'://判断类别名称是否重复
					if(!empty($_GET['cat_id']))
					{
						$count  = $this->category_c->get_category_info($w_type,$_GET['cat_id'],0,$_GET['cat_name'],'cat_name',
						'','',(int)$_GET['type']);
					
					}else
					{
						$count  = $this->category_c->get_category_info($w_type,0,0,$_GET['cat_name'],'cat_name','','',
						(int)$_GET['type']);
					}
					echo  $count;
				break;
			
				case 'update_cat_related'://修改相关连的话题
					$cat_str =  $_GET['cat_str'];
					if(substr($_GET['cat_str'], -1) == ',')
					{
						$cat_str = substr($_GET['cat_str'], 0, strlen($_GET['cat_str']) - 1) ;
					}
					$this->category_c->get_category_info($w_type,(int)$_GET['cat_id'],0,'','cat_related',
					$cat_str);
				break;
			
				case 'del_category'://删除分类
					if (!empty($_GET['cat_id']))
					{
						$rows = $this->category_c->del_category((int)$_GET['cat_id'],$w_type);
						if($rows != -1)
						{
							$cat_ex = explode(',',$rows);
							foreach ($cat_ex as $key => $value) 
							{
							$cat_arr[]['cat_id'] = $value;
							}
							echo json_encode($cat_arr) ;
						}else 
						{
							echo -1;
						}
					}
				break;
			
				case 'update_category'://修改分类信息
					echo $this->category_c->update_category($w_type,$_REQUEST['cat_name'],$_REQUEST['parent_id'],
					$_REQUEST['sort_order'],$_REQUEST['type'],$_REQUEST['cat_intro'],
					$_REQUEST['cat_id'],$_REQUEST['cat_image'],$_REQUEST['cat_desc'],
					(int)$_REQUEST['recommended'],trim($_REQUEST['cat_thumb']),trim($_REQUEST['google_code_article']),
					trim($_REQUEST['google_code_guide']), trim($_REQUEST['google_code_buy']),trim($_REQUEST['google_code_oview']),
					trim($_REQUEST['google_code_install']), trim($_REQUEST['google_code_uninstall']),trim($_REQUEST['google_code_register']),
					$_REQUEST['is_hot']);
					
					//定义更新分类模板
					$update_arr = array('category_id' => $_REQUEST['cat_id'],
										'cat_series' => $_REQUEST['cat_series'],
										'category_type' => (int) $_REQUEST['type'],
										'w_type' => (int) $w_type,
										'is_ajax' => 1,
										'operation_type' => 'category',				
					);
				
					file_get_contents("http://cms.wondershare.com/wondershare/template.php?action=updateTemplate&type=update&".
										http_build_query($update_arr));
					
				break;
			
				case 'add_category'://增加分类信息
					
					echo  $this->category_c->update_category($w_type,$_REQUEST['cat_name'],$_REQUEST['parent_id'],
					$_REQUEST['sort_order'],$_REQUEST['type'],$_REQUEST['cat_intro'],0,
					$_REQUEST['cat_image'],$_REQUEST['cat_desc'],(int)$_REQUEST['recommended'],
					trim($_REQUEST['cat_thumb']),trim($_REQUEST['google_code_article']),
					trim($_REQUEST['google_code_guide']), trim($_REQUEST['google_code_buy']),trim($_REQUEST['google_code_oview']),
					trim($_REQUEST['google_code_install']), trim($_REQUEST['google_code_uninstall']),trim($_REQUEST['google_code_register']),
					$_REQUEST['is_hot']);
					
					
					
				break;
			}
			exit;
	
		}else if($_REQUEST['act'] == 'edit')//编辑分类
		{ 
			$category_info = $this->category_c->get_category_info($w_type,(int)$_GET['cat_id']);
			
			$GLOBALS['smarty']->assign('web_info',   get_web_info($w_type));
			$GLOBALS['smarty']->assign('w_type',   $w_type);
			$GLOBALS['smarty']->assign('form_act',     'edit');
			$GLOBALS['smarty']->assign('form_value',     '修改');
			$GLOBALS['smarty']->assign('cat_info',     $category_info);
			$GLOBALS['smarty']->assign('type',     $_GET['type']);
			$GLOBALS['smarty']->assign('cat_select',   cat_list(0, $category_info[0]['parent_id'], true,0,true,$where,0));
			$GLOBALS['smarty']->display('category_info.htm');
			
		} else if($_REQUEST['act'] == 'add')//增加分类
		{
			$GLOBALS['smarty']->assign('web_info',   get_web_info($w_type));
			$GLOBALS['smarty']->assign('w_type',   $w_type);
			$GLOBALS['smarty']->assign('type',     $_GET['type']);
			$GLOBALS['smarty']->assign('form_act',     'add');
			$GLOBALS['smarty']->assign('form_value',     '增加');
			$GLOBALS['smarty']->assign('cat_select',   cat_list(0, 0, true,0,true,$where));
			$GLOBALS['smarty']->display('category_info.htm'); 
		} else { //列表
			
			$cat_info = $all_checkbox = array();
			$cat_info  = cat_list(0, 0, false, 0, true, $where);
			if($_GET['type'] == 1)//代表为文章分类列表
			{
				$all_checkbox = $this->category_c->get_category_info($w_type,0,0,'','all_cat_box','',0,$_GET['type']);
				foreach ($cat_info as $key=>$value)
				{
					if($value['cat_series'] == 3)
					{
						$value['cat_related'] = substr($value['cat_related'], -1) == ','?substr($value['cat_related'], 0, strlen($value['cat_related']) - 1 ):$value['cat_related'];	
						$arr_related = explode(',',$value['cat_related']);
						$cat_info[$key]['cat_related'] =  $this->get_checkbox($arr_related,$all_checkbox,$value['cat_id']);
					}else
					{
						$cat_info[$key]['cat_related'] = '';
					}
				}
			}else if($_GET['type'] == 2)//代表为ws产品分类列表
			{
				foreach ($cat_info as $key=>$value)
				{
					if($value['is_c_mobile'] == 0 && $value['cat_series'] == 3 )
					{
					$all_checkbox = $this->category_c->get_category_info($w_type,$value['cat_id'],0,'','all_cat_box','',0,$_GET['type']);
					$all_checkbox = array_unique($all_checkbox);
					$value['cat_related'] = substr($value['cat_related'], -1) == ','?substr($value['cat_related'], 0, strlen($value['cat_related']) - 1 ):$value['cat_related'];
					$cat_info[$key]['cat_related'] =  $this->get_checkbox($value['cat_related'],$all_checkbox,$value['cat_id'],'radio');
					}
				}
			}
			
			unset($all_checkbox);
			
			$GLOBALS['smarty']->assign('web_info',   get_web_info($w_type));
			$GLOBALS['smarty']->assign('w_type',   $w_type);
			$GLOBALS['smarty']->assign('type',     $_GET['type']);
			$GLOBALS['smarty']->assign('cat_info',     $cat_info);
			$GLOBALS['smarty']->assign('form_value',     '增加');
			$GLOBALS['smarty']->assign('site_config', $GLOBALS['site_config'][$w_type]['category'] );
			$GLOBALS['smarty']->display('category_index.htm'); 
		}
	}
	/**
	* 得到checkbox组合
	*
	* @param   string        $attributes_id      产品属性
	* @param   string        $all_attributes     属性表所有属性
	* @param   int            $cat_id            产品id
	* @param   string        $input_type         input 类型
	* @return  string
	*/
	function get_checkbox($select_arr_id,$all_checkbox,$cat_id,$input_type = 'checkbox')
	{
		$str = $input_name = '';
		if($input_type == 'radio')
		{
			$input_name = 'product'.$cat_id;
			for($i=0;$i<count($all_checkbox);$i++)
			{
				if($all_checkbox[$i]['cat_id'] != $cat_id)
				{
					if($select_arr_id == $all_checkbox[$i]['cat_id'])
					{
						$str.='<input type='.$input_type.' name='.$input_name.' value='.$all_checkbox[$i]['cat_id'].'  
						title = "'.$all_checkbox[$i]['cat_title'].'" checked="checked"/>'.$all_checkbox[$i]['cat_title'].' &nbsp;&nbsp;';
					}else
					{
						$str.='<input type='.$input_type.' name='.$input_name.' value='.$all_checkbox[$i]['cat_id'].' 
						title = "'.$all_checkbox[$i]['cat_title'].'" />'.$all_checkbox[$i]['cat_title'].' &nbsp;&nbsp;';
					}
				}
			}
		} else if($input_type == 'checkbox') {
			
			$input_name = 'topic'.$cat_id.'[]';
			for($i=0;$i<count($all_checkbox);$i++)
			{
				if($all_checkbox[$i]['cat_id'] != $cat_id)
				{
					$fage = 1;
					foreach ($select_arr_id as $key=>$value)
					{
						if($value == $all_checkbox[$i]['cat_id'])
						{
							$fage = 0;
							$str.='<input type='.$input_type.' name='.$input_name.' value='.$all_checkbox[$i]['cat_id'].'  
							title = "'.$all_checkbox[$i]['cat_title'].'" checked="checked"/>'.$all_checkbox[$i]['cat_title'].' &nbsp;&nbsp;';
						}
					}
					if($fage == 1)
					{
						$str.='<input type='.$input_type.' name='.$input_name.' value='.$all_checkbox[$i]['cat_id'].' 
						title = "'.$all_checkbox[$i]['cat_title'].'" />'.$all_checkbox[$i]['cat_title'].' &nbsp;&nbsp;';
					}
				}
			}
		}
		
		return $str;
	}
}
$action    = $_REQUEST['action']; 
$category   = new Category;
if(method_exists($category, $action)) 
{
$category->$action();
}else{
$category->index();
}




?>
